<?php

defined('SYSPATH') or die('Access error!');

class Controller_Shop_User_Order extends Controller_Shop_User_Main
{

    protected $order;
    protected $order_id = 0;
    protected $user_type = '';
    protected $data = array();

    public function before()
    {
        parent::before();
        $this->order = Model::factory('shop_order');
        Assets::css('base', $this->template->media_path . 'css/base.css');
        I18n::set_current_file('shop');
    }

    public function order_list()
    {
        //当前位置
        $this->set_define_position(__($this->user_type . ' manager'));
        Breadcrumbs::add(__('order list'), '');
        $order_list = $this->order->get_order_list($this->data);
        $this->template->assign('order_list', $order_list['list']);
        $this->template->assign('page', $order_list['page']);
        $this->template->view('shop_order_' . $this->user_type . '_list');
    }

    public function order_view()
    {
        $id = intval($this->order_id);

        if (empty($id) || $id == 0)
        {
            Message::error(__('no exist'));
            $this->request->redirect(URL::site('shop/' . $this->user_type . '/order/list'));
        }
        $order_detail = $this->order->get_order_detail($id);
        if (!Arr::validate_arr($order_detail))
        {
            Message::error(__('no exist'));
            $this->request->redirect(URL::site('shop/' . $this->user_type . '/order/list'));
        }
        $order_detail['user_type'] = $this->user_type;
        //当前位置
        $this->set_define_position(__($this->user_type . ' manager'));
        Breadcrumbs::add(__('order view'), '');
        $this->check_pre($order_detail[$this->user_type . '_userid']);
        $this->template->assign('_token', CSRF::set_csrf_token());
        $this->template->assign('data', $order_detail);
        $this->template->view('shop_order_view');
    }

    public function order_operate()
    {
        $order_id = Arr::get($_POST, 'order_id', '');
        $order_detail = $this->order->get_order_detail($order_id);

        if (empty($order_id) || !Arr::validate_arr($order_detail))
        {
            Message::error(__('no exist'));
            $this->request->redirect(URL::site('shop/' . $this->user_type . '/order/list'));
        }


        if (isset($_POST['_token']) && !CSRF::check_csrf_token($_POST['_token']))
        {
            Message::error('已失效，请重新操作');
            $this->request->redirect(URL::site('shop/' . $this->user_type . '/order/view/' . $order_id));
        }

        $this->check_pre($order_detail[$this->user_type . '_userid']);

        $status = Arr::get($_POST, 'pay_status', $order_detail['status']);

        if ($this->order->update(array('status' => $status), array('id' => $order_id)))
        {
            if ($this->user_type == 'buyer' || $status = 'payed')
            {
                //更改用户的积分
                $this->handle_pay_account($this->user_detail['id'], $order_detail['account_type'], $order_detail['order_account'], $order_id);
            }
            //更改订单日志
            Model::factory('shop_order_log')->insert($this->handle_log_info($order_id, $order_detail['status'], $status));
            Message::success('操作成功');
            $this->request->redirect(URL::site('shop/' . $this->user_type . '/order/view/' . $order_id));
        }
    }

    public function check_pre($userid)
    {
        if ($userid != $this->user_detail['id'])
        {
            Message::error(__('no exist'));
            $this->request->redirect(URL::site('shop/' . $this->user_type . '/order/list/'));
        }
    }

    public function handle_log_info($order_id, $now_status, $change_status)
    {
        return array(
            'order_id' => $order_id,
            'operator' => $this->user_detail['username'],
            'order_status' => $now_status,
            'changed_status' => $change_status,
            'log_time' => Date::get_time(),
        );
    }

    public function handle_pay_account($user_id, $account_type, $account, $order_id)
    {
        switch ($account_type)
        {
            case 'points':
                return Model_User_Points::factory('user_points')->save_points($user_id, array('order_buy_goods' => $account), $order_id);
                break;
            default:
                break;
        }
    }

}